"""
Copyright 2020 Huawei Technologies Co., Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import time

import ascendfaiss
import datasets

start_time = time.time()

print("[%.3f] Reading data from SIFT1M" % (time.time() - start_time))
base, query, train, ground_truth = datasets.load_sift1M('./sift1M')
SCALE = 1.0 / 128.0

d = base.shape[1]    # vector dims
nb = base.shape[0]   # database size
nt = train.shape[0]  # tain set size
nq = query.shape[0]  # query size

# preprocessing, scale to avoid fp16 overflow
print("Scale by %f" % SCALE)
base = base * SCALE
train = train * SCALE
query = query * SCALE

# set running devices
DEVICE = [0]
dev = ascendfaiss.IntVector()
for i in DEVICE:
    dev.push_back(i)
config = ascendfaiss.AscendIndexSQConfig(dev)

# create sq index
ascend_index_sq = ascendfaiss.AscendIndexSQ(
    d, ascendfaiss.ScalarQuantizer.QT_8bit,
    ascendfaiss.METRIC_L2, config)
ascend_index_sq.verbose = True

# train
ascend_index_sq.train(train)

# add database
ascend_index_sq.add(base)


# search topk results
k = 1000

for i in [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]:
    t, r = datasets.evaluate(ascend_index_sq, query, ground_truth, k)
    print("@%3d qps: %.3f, r@1: %.4f, r@10: %.4f, r@100: %.4f, r@1000: %.4f" %
          (i, 1000.0 / t, r[1], r[10], r[100], r[1000]))
